package com.animee.tally;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.animee.tally.adapter.AccountAdapter;
import com.animee.tally.db.AccountBean;
import com.animee.tally.db.DBManager;
import com.animee.tally.util.CalenderDialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
/*  显示历史账单的acitivity*/
public class HistoryActivity extends AppCompatActivity {
    TextView dateTv;
    ListView infoLv;
    //数据源
    List<AccountBean>mDatas;
    AccountAdapter adapter;

    int year,month;
    int dialogSelPos = -1;
    int dialogSelMonth = -1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);   //将activity和布局相关联的方法
        dateTv = findViewById(R.id.history_tv_time);
        infoLv = findViewById(R.id.history_lv);
        mDatas = new ArrayList<>();
        //设置适配器
        adapter = new AccountAdapter(this, mDatas);
        infoLv.setAdapter(adapter);

        // 加载数据的方法
        Calendar calendar = Calendar.getInstance();
         year = calendar.get(Calendar.YEAR);
         month = calendar.get(Calendar.MONTH)+1;
        loadData(year,month);
        dateTv.setText(year+"年"+month+"月");
        setLVClickListener();
    }
    /* 设置ListView当中每一个item点击的函数*/
    private void setLVClickListener() {
        infoLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                final AccountBean bean = mDatas.get(position);
                final int delId = bean.getId();
                //弹出对话框，提示用户是否确定要删除
                AlertDialog.Builder builder = new AlertDialog.Builder(HistoryActivity.this);
                builder.setTitle("提示信息").setMessage("您确定要删除这条记录么?")
                        .setNegativeButton("取消",null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                DBManager.deleteItemFromAccountById(delId);
                                mDatas.remove(bean);    //从集合当中移除对象
                                adapter.notifyDataSetChanged();
                            }
                        });
                builder.create().show();
                return false;
            }
        });
    }

    private void loadData(int year,int month) {
        List<AccountBean> list = DBManager.getAccountListOneMonth(year, month);
        mDatas.clear();    //原来集合内容清空
        mDatas.addAll(list);  // 添加新数据
        adapter.notifyDataSetChanged();
    }
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.history_iv_back:
                finish();
                break;
            case R.id.history_iv_rili:
                // 弹出日历对话框
                CalenderDialog dialog = new CalenderDialog(this,dialogSelPos,dialogSelMonth);
                dialog.show();
                dialog.setDialogSize();
                dialog.setOnRefreshListener(new CalenderDialog.OnRefreshListener() {
                    @Override
                    public void onRefresh(int selectPos, int year, int month) {
                        dateTv.setText(year+"年"+month+"月");
                        loadData(year,month);
                        dialogSelPos = selectPos;
                        dialogSelMonth = month;
                    }
                });
                break;

        }
    }
}
